package o;

import android.content.ContentValues;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.datastorage.database.EnableDatabase;
import com.huawei.appgallery.datastorage.database.impl.process.DefaultProcess;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;

public abstract class bmd {
    private static String ˏ(blw o_blw, String str) {
        Field[] ˏ = bme.ˏ(o_blw.getClass());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("create table ");
        stringBuilder.append(str);
        stringBuilder.append(" ( ");
        stringBuilder.append("_id INTEGER primary key autoincrement ");
        for (Field field : ˏ) {
            boolean isAccessible = field.isAccessible();
            field.setAccessible(true);
            String name = field.getName();
            if (field.isAnnotationPresent(EnableDatabase.class)) {
                bmc ˊ = ˊ(field);
                if (ˊ == null) {
                    field.setAccessible(isAccessible);
                    bmg.ˊ.ˎ("DataSourceUtil", "unsupport type:" + field.getType());
                } else {
                    String ˊ2 = ˊ.ˊ();
                    if (ˊ2 != null) {
                        stringBuilder.append(" , ");
                        stringBuilder.append(name);
                        stringBuilder.append(HwAccountConstants.BLANK);
                        stringBuilder.append(ˊ2);
                    }
                    field.setAccessible(isAccessible);
                }
            } else {
                field.setAccessible(isAccessible);
            }
        }
        stringBuilder.append(" ) ");
        return stringBuilder.toString();
    }

    @NonNull
    public static String ˎ(blw o_blw) {
        return ˏ(o_blw, o_blw.ˊ());
    }

    @NonNull
    public static ContentValues ˋ(blw o_blw) {
        Field[] ˏ = bme.ˏ(o_blw.getClass());
        ContentValues contentValues = new ContentValues();
        int length = ˏ.length;
        int i = 0;
        while (i < length) {
            Field field = ˏ[i];
            boolean isAccessible = field.isAccessible();
            try {
                field.setAccessible(true);
                String name = field.getName();
                if (field.isAnnotationPresent(EnableDatabase.class)) {
                    Object obj = field.get(o_blw);
                    if (obj == null) {
                        field.setAccessible(isAccessible);
                    } else {
                        bmc ˊ = ˊ(field);
                        if (ˊ == null) {
                            field.setAccessible(isAccessible);
                        } else {
                            ˊ.ˋ(contentValues, name, obj);
                            field.setAccessible(isAccessible);
                        }
                    }
                    i++;
                } else {
                    i++;
                }
            } catch (Throwable e) {
                bmg.ˊ.ˋ("DataSourceUtil", "beanToContentValues IllegalAccessException", e);
                i++;
            } catch (Throwable e2) {
                bmg.ˊ.ˋ("DataSourceUtil", "beanToContentValues Exception", e2);
                i++;
            } finally {
                field.setAccessible(isAccessible);
            }
        }
        return contentValues;
    }

    public static void ॱ(blw o_blw, Cursor cursor) {
        Field[] ˏ = bme.ˏ(o_blw.getClass());
        int length = ˏ.length;
        int i = 0;
        while (i < length) {
            Field field = ˏ[i];
            boolean isAccessible = field.isAccessible();
            try {
                field.setAccessible(true);
                String name = field.getName();
                if (field.isAnnotationPresent(EnableDatabase.class)) {
                    int columnIndex = cursor.getColumnIndex(name);
                    if (columnIndex == -1) {
                        field.setAccessible(isAccessible);
                    } else {
                        bmc ˊ = ˊ(field);
                        if (ˊ == null) {
                            field.setAccessible(isAccessible);
                        } else {
                            ˊ.ˎ(o_blw, field, cursor, columnIndex);
                            field.setAccessible(isAccessible);
                        }
                    }
                    i++;
                } else {
                    i++;
                }
            } catch (Throwable th) {
                bmg.ˊ.ˋ("DataSourceUtil", "cursorToBean Exception", th);
                i++;
            } finally {
                field.setAccessible(isAccessible);
            }
        }
    }

    private static bmc ˊ(Field field) {
        Class process = ((EnableDatabase) field.getAnnotation(EnableDatabase.class)).getProcess();
        if (process != DefaultProcess.class) {
            try {
                return (bmc) process.newInstance();
            } catch (IllegalAccessException e) {
                bmg.ˊ.ˎ("DataSourceUtil", "getTypeProcess failed:IllegalAccessException");
                return null;
            } catch (InstantiationException e2) {
                bmg.ˊ.ˎ("DataSourceUtil", "getTypeProcess failed:InstantiationException");
                return null;
            }
        }
        bmc o_bmc = (bmc) bmi.ˏ.get(field.getType());
        if (o_bmc != null) {
            return o_bmc;
        }
        for (Entry entry : bmi.ˊ.entrySet()) {
            if (((Class) entry.getKey()).isAssignableFrom(field.getType())) {
                return (bmc) entry.getValue();
            }
        }
        return o_bmc;
    }

    @Nullable
    public static blw ॱ(Class<? extends blw> cls) {
        Constructor declaredConstructor;
        boolean isAccessible;
        Constructor constructor;
        blw o_blw;
        boolean z = false;
        try {
            declaredConstructor = cls.getDeclaredConstructor(new Class[0]);
            try {
                isAccessible = declaredConstructor.isAccessible();
                constructor = declaredConstructor;
            } catch (NoSuchMethodException e) {
                bmg.ˊ.ˏ("DataSourceUtil", "table failed:NoSuchMethodException");
                isAccessible = z;
                constructor = declaredConstructor;
                if (constructor != null) {
                    try {
                        constructor.setAccessible(true);
                        o_blw = (blw) constructor.newInstance(new Object[0]);
                        return o_blw;
                    } catch (IllegalAccessException e2) {
                        o_blw = bmg.ˊ;
                        o_blw.ˏ("DataSourceUtil", "table failed:IllegalAccessException");
                    } catch (InstantiationException e3) {
                        o_blw = bmg.ˊ;
                        o_blw.ˏ("DataSourceUtil", "table failed:InstantiationException");
                    } catch (InvocationTargetException e4) {
                        o_blw = bmg.ˊ;
                        o_blw.ˏ("DataSourceUtil", "table failed:InvocationTargetException");
                    } finally {
                        constructor.setAccessible(isAccessible);
                    }
                }
                return null;
            }
        } catch (NoSuchMethodException e5) {
            declaredConstructor = null;
            bmg.ˊ.ˏ("DataSourceUtil", "table failed:NoSuchMethodException");
            isAccessible = z;
            constructor = declaredConstructor;
            if (constructor != null) {
                constructor.setAccessible(true);
                o_blw = (blw) constructor.newInstance(new Object[0]);
                return o_blw;
            }
            return null;
        }
        if (constructor != null) {
            constructor.setAccessible(true);
            o_blw = (blw) constructor.newInstance(new Object[0]);
            return o_blw;
        }
        return null;
    }
}
